package org.whatever.aha.zjut.platform.service.judge;

import cn.dev33.satoken.stp.StpUtil;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.whatever.aha.zjut.base.constant.ErrorCode;
import org.whatever.aha.zjut.base.constant.StatusConstant;
import org.whatever.aha.zjut.base.exception.AppException;
import org.whatever.aha.zjut.platform.annotation.StageLimit;
import org.whatever.aha.zjut.platform.dto.school.ProjectScoreDto;
import org.whatever.aha.zjut.platform.entity.school.JudgeProjectDistribution;
import org.whatever.aha.zjut.platform.entity.school.ProjectScore;
import org.whatever.aha.zjut.platform.service.competition.CompetitionService;
import org.whatever.aha.zjut.platform.service.competition.CompetitionStageAwardService;
import org.whatever.aha.zjut.platform.service.competition.CompetitionStageService;
import org.whatever.aha.zjut.platform.service.project.ProjectService;
import org.whatever.aha.zjut.platform.service.school.JudgeProjectDistributionService;
import org.whatever.aha.zjut.platform.service.school.ProjectScoreService;
import org.whatever.aha.zjut.platform.util.StatusConvertUtil;
import org.whatever.aha.zjut.platform.vo.PageVo;
import org.whatever.aha.zjut.platform.vo.competition.CompetitionStageRoughInfoVo;
import org.whatever.aha.zjut.platform.vo.competition.CompetitionStageVo;
import org.whatever.aha.zjut.platform.vo.judge.JudgeProjectRoughInfoVo;
import org.whatever.aha.zjut.platform.vo.judge.StandardAndAwardInfoVo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Vc
 * @version 1.0
 * @Desc 评委相应操作
 * @date 2021/11/24 19:50
 */
@RequiredArgsConstructor
@Service
public class JudgeService {
    final ProjectScoreService projectScoreService;
    final JudgeProjectDistributionService judgeProjectDistributionService;
    final CompetitionStageService competitionStageService;
    final CompetitionService competitionService;
    final CompetitionStageAwardService competitionStageAwardService;
    final ProjectService projectService;

    /**
     * 评委进行打分  已经评了的不能评  评审在评审阶段进行评分  已经提交公示结果了不能改
     *
     * @param projectScoreDto
     * @return
     */
    public int addProjectScore(ProjectScoreDto projectScoreDto){
        return projectScoreService.addProjectScore(projectScoreDto);
    }

    /**
     * 评委修改打分评论和最后更新时间
     *
     * @param projectScoreDto
     * @return
     */
    public int modifyProjectScore(ProjectScoreDto projectScoreDto){
        return projectScoreService.modifyProjectScore(projectScoreDto);
    }

    /**
     * 获取登录评委要审核的竞赛信息
     *
     * @return
     */
    public Map getJudgingCompInfo(int userId){
        List<JudgeProjectDistribution> judgeProjectDistributionInfo = judgeProjectDistributionService.getJudgeProjectDistributionByJudgeId(userId);
        List<CompetitionStageRoughInfoVo> alreadyJudgedCompInfo = new ArrayList<>();
        List<CompetitionStageRoughInfoVo> notJudgeCompInfo = new ArrayList<>();
        judgeProjectDistributionInfo.stream().forEach(judgeProjectDistribution -> {
            CompetitionStageRoughInfoVo competitionStageRoughInfoVo = competitionService.getCompetitionRoughByCompIdAndStageId(judgeProjectDistribution.getCompId(), judgeProjectDistribution.getStageId());
            //如果阶段状态不是进行中则归为评审完成的范畴
            if(StatusConstant.COMPETITION_STAGE_STATUS_ONGOING.getCode() == competitionStageService.getStageStatusByTime(judgeProjectDistribution.getStageId())){
                notJudgeCompInfo.add(competitionStageRoughInfoVo);
            }else {
                alreadyJudgedCompInfo.add(competitionStageRoughInfoVo);
            }
        });
        return Map.of("notJudgeCompInfo", notJudgeCompInfo, "alreadyJudgedCompInfo", alreadyJudgedCompInfo);
    }

    /**
     * 获取竞赛标准和获奖信息
     *
     * @param compId
     * @param stageId
     */
    public StandardAndAwardInfoVo getJudgeStandardAndAwardInfo(int compId, int stageId){
        StandardAndAwardInfoVo standardAndAwardInfoVo = new StandardAndAwardInfoVo();
        standardAndAwardInfoVo.setAwardInfoVo(competitionStageAwardService.getCompStageAwardListById(stageId));
        standardAndAwardInfoVo.setStandard(competitionService.getStandardByCompId(compId));
        return standardAndAwardInfoVo;
    }


}
